#!/bin/sh

## hyphop ##

#= downloader

#GET(){

PP=$(dirname $0)
PR=$(realpath $PP)

    [ "$1" ] || { 
	echo "[w] undef src">&2
	return 1
    }

OK(){
	[ -s "$O_" ] || {
	    echo "[e] $O_ not found">&2
	    exit 1
	}

	md5sum "$O_" > "$O_.md5sum"
	cd $P_
	exit 0
#	return 0
}

    O_="$2"
    [ "$O_" ] || O_=$(basename "$1")
    D_="$3"
    [ "$D_" ] || D_="$DOWNLOADS"

    echo "[i] DOWNLOAD $1 => $O_ DIR $D_">&2

    T_=/tmp/dl.head
    P_=$(pwd)
    [ $D_ ] && {
	cd $D_ || { echo "[e] change dir to $D_">&2
	exit 1
        }
    }
    [ -f "$O_.md5sum" ] && {
	echo "[w] already downloaded">&2
	cd $P_
	return 0
    }

    case "$1" in
	*//mega.nz*)
	[ "$megadl" ] || {
	    echo "[i] mega donwload $1">&2
#	    megadl=$(which megadl)
#	    use last version
	    megadl=$(which megatools)
	    [ "$megadl" ] || {
		echo "[w] megatools not found!!! try use ./tc/make_070_megatools ">&2

	    $PR/tc/make_070_megatools.sh

	    megadl=$(which megatools)

	    [ "$megadl" ] || {
		echo "[w] megatools build fail">&2
		exit 1
	    }
	    }
	    megadl="$megadl dl"
	}
	

	$megadl --print-names "$1" | tee $T_

	o_=$(tail -1 "$T_")

	case "$o_" in
	    /*)
	    echo "[i] mega downlaoded $(basename $o_)">&2
	    ;;
	    *)
	    echo "[e] mega download error">&2
	    return 1
	esac

	[ "$o_" = "$O_" ] || \
	mv "$o_" "$O_"
	;;
	*//*)
	curl -f -jkL -C- -D "$O_.headers" "$1" -o "$O_"
	r_=$?
	case $r_ in
	    22)
	    echo "[i] curl ret code $r_">&2
	    head -n1 "$O_.headers" | grep 416 && {
	    echo "[i] file already downloaded">&2
	    ##curl: (22) The requested URL returned error: 416
	    OK
	    }
	    ;;
	    0)
	    OK
	    ;;
	    *)
	    echo "[i] curl ret code $r_">&2
	    cat "$O_.headers">&2
	    ;;
	esac
	#echo "[i] curl ret $r_">&2
	return $r_
	#exit $r_
	;;
    esac

    [ "$r_" = "0" ] && OK

    cd $P_

#}
#
#GET "$LINK" "$IMG" $DL || DIE

